package com.billing.Interceptor;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.billing.model.entity.ManagerUsersBean;
import com.billing.model.entity.TUser;
import com.billing.service.UserService;
import com.billing.web.controller.FilterEnum;
import com.billing.web.controller.IdentityController;
import com.billing.web.controller.IdentityEnum;

public class LoginInterceptor implements HandlerInterceptor{

	
	@Resource
	private UserService userService;
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		JSONObject jsonObject = null;
		String url=request.getRequestURI();
		url=url.substring(url.lastIndexOf("/")+1, url.length());
		String code=IdentityEnum.getIdentityEnum(url);
		if(code==null||code==""){
			TUser user = (TUser) request.getSession().getAttribute("user");
			if(user != null){
				return true;
			}
			response.sendRedirect("../login.jsp");
		}else{
			String filter=FilterEnum.getFilterEnum(code);
			if(filter==null||filter==""){
				Map<String, Object> remap=new HashMap<String, Object>();//封装返回信息
				JSONObject jsonmsg =null;//返回msg信息
				String msg=null;
				String userid=null;
				String sign=null;
				String json = request.getParameter("json");
				response.setCharacterEncoding("UTF-8");  
				response.setContentType("application/json; charset=utf-8");
				PrintWriter out = null; 
				try {
					json=java.net.URLDecoder.decode(json,"UTF-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				jsonObject = JSONObject.fromObject(json);
				try{
					userid =jsonObject.getString("userid");
					sign =jsonObject.getString("sign");
				}catch(Exception e){
					msg="{\"code\":\"9002\",\"msg\":\"参数有误"+e.getMessage().replaceAll("\"", "")+"\"}";
					out = response.getWriter();  
					out.append(msg);
					return false;
				}
				//判断用户信息
				TUser user=userService.selectTUserId(userid);
				if (user == null) {
					remap.put("code", "3");
					remap.put("msg", "用户信息有误");
					jsonmsg = JSONObject.fromObject(remap);
					out = response.getWriter();  
					out.append(jsonmsg.toString());
					return false;
				}
				boolean judge=IdentityController.authentication(user, code, sign);
				if(!judge){
					remap.put("code", "3");
					remap.put("msg", "用户身份信息有误");
					jsonmsg = JSONObject.fromObject(remap);
					out = response.getWriter();  
					out.append(jsonmsg.toString());
					return false;
				}
			}
			return true;
		}
		return false;
	}

}
